<?php
/**
 * Created by PhpStorm.
 * User: chenguangpeng
 * Date: 15-9-21
 * Time: 下午2:12
 */
namespace PFT\SMS;
use \TopClient;
use \AlibabaAliqinFcSmsNumSendRequest;
include "/var/www/html/new/d/module/taobao/TopSdk.php";
class TaoBaoSms {
    const APP_KEY    = '23232967';
    const APP_SECRET = '13ef824b903fae7e56a9b78055e38f82';

    const TPL_ORDER_NO_LNK              = 'SMS_300001';//不带二维码地址的通知
    const TPL_ORDER_WITH_LNK            = 'SMS_290005';//带二维码地址的订单通知
    const TPL_ORDER_HOTEL_CUSTOMER      = 'SMS_300004';//酒店订单通知游客
    const TPL_ORDER_HOTEL_APPLY         = 'SMS_300003';//酒店订单通知单供应商
    const TPL_FORGET_PWD                = 'SMS_280002';//忘记密码
    const TPL_RESET_PWD                 = 'SMS_285002';//重置密码
    const TPL_UPGRADE_ACCOUNT           = 'SMS_300002';//账号升级通知
    const TPL_CHANGE_TEL                = 'SMS_280003';//修改手机号通知
    const TPL_VCODE_REG                 = 'SMS_285003';//注册发送验证码通知
    const TPL_ADD_DISTRIBUTOR           = 'SMS_280005';//添加分销商通知
    const TPL_MEMBER_CARD_CONSUME       = 'SMS_300005';//会员卡消费通知
    const TPL_TRANSFER_MONEY            = 'SMS_285005';//用户平台转账通知


    /**
     * 发送短信
     *
     * @param $tel string 手机号
     * @param $params string json参数
     * @param $sms_sign string 短信签名
     * @param $sms_tpl_id string 短信模板ID
     * @return mixed|\ResultSet|\SimpleXMLElement
     */
    public static function doSendSMS($tel, $params, $sms_sign, $sms_tpl_id){
        $client = new TopClient(self::APP_KEY, self::APP_SECRET);

        $req = new AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("");
        $req->setSmsType("normal");
        $req->setSmsFreeSignName($sms_sign);
        $req->setSmsParam($params);
        $req->setRecNum($tel);
        $req->setSmsTemplateCode($sms_tpl_id);
        $resp = $client->execute($req, '');
        return $resp;
    }

    /**
     * 发送订单短信
     *
     * @param $tel
     * @param $sms_sign
     * @param $code
     * @param $pname
     * @param $tnum
     * @param $begintime
     * @param $getaddr
     * @param string $qr_link
     * @return array|int
     */
    public static function sendOrder($tel, $sms_sign, $code, $pname, $tnum, $begintime, $getaddr, $qr_link='')
    {
        self::Log("发送短信开始");
//        凭证号:${code}，您已成功购买了${pname}${tnum},消费日期:${begintime}，${getaddr}，此为凭证，请妥善保管。
        $json = '{"code":"'.$code.'", "pname":"'.$pname.'","tnum":"'.$tnum.'"'
            .',"begintime":"'.$begintime.'","getaddr":"'.$getaddr.'"';
        if (!$qr_link) {
            $tpl_id = self::TPL_ORDER_NO_LNK;
        }
        else {
            $json .= ',"link":"'.$qr_link.'"';
            $tpl_id = self::TPL_ORDER_WITH_LNK;
        }
        $json .= '}';

        $ret = self::doSendSMS($tel, $json, $sms_sign, $tpl_id);
        self::Log("tpl_id={$tpl_id}|$sms_sign|json={$json}|ret=".json_encode($ret));
        if ($ret->result->success==true) {
            return array('num'=>100, 'content'=>$tpl_id.'|'.$json);
        }
        return 0;
    }

    public static function Log($txt)
    {
        $fp = fopen('/var/www/html/new/d/logs/api/hongqunsms/taobao_log.txt',"a");
        flock($fp, LOCK_EX);
        fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
        flock($fp, LOCK_UN);
        fclose($fp);
    }
} 